package com.demo.goodsinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.demo.core.ElasticSearchTransportClient;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {

	/**
	 * https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.3/index.html
	 */
	@Autowired
	ElasticSearchTransportClient elasticSearchTransportClient;
	
	@Autowired
	ObjectMapper objectMapper;

	@Override
	public List<Object> queryDatas(String queryString) {
		List<Object> list = new ArrayList<Object>();
		try {
			QueryBuilder nameQueryBuilder = QueryBuilders
					.queryStringQuery(queryString);//

			SearchRequestBuilder searchRequestBuilder = elasticSearchTransportClient
					.getInstance().prepareSearch();//
			SearchResponse nameSearchResponse = searchRequestBuilder
					.setQuery(nameQueryBuilder).execute().get();
			SearchHits hits = nameSearchResponse.getHits();
			Iterator<SearchHit> it = hits.iterator();
			while (it.hasNext()) {
				SearchHit shit = it.next();
				Map<String, Object> map = objectMapper.readValue(shit.getSourceAsString(), new TypeReference<HashMap<String,Object>>(){});
				//Map<String, Object> map = objectMapper.readValue(shit.getSourceAsString(), HashMap.class);
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(queryString+"\t"+list.size());
		return list;
	}

	private static final String index = "goods_index";
	private static final String type = "goods_info";
	
	//http://10.8.48.156:9200/goods_index/_search
	@Override
	public void saveDatas(List<Map<String, Object>> datas) {
		TransportClient client = elasticSearchTransportClient.getInstance();
		BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();

		if (datas != null) {
			for (int i = 0; i < datas.size(); i++) {
				try {
					bulkRequestBuilder.add(client.prepareIndex(index, type)
							.setSource(objectMapper.writeValueAsString(datas.get(i))));
				} catch (JsonProcessingException e) {
					e.printStackTrace();
				}
			}
		}
		BulkResponse response = bulkRequestBuilder.get();
		if(response.buildFailureMessage()!=null && response.buildFailureMessage().length()>0){
			System.err.println(response.buildFailureMessage());
		}
	}
	
	@Autowired
	JdbcTemplate mysqlJdbcTemplate;

	@Override
	public List<Map<String, Object>> getDatasFromDB() {
		return mysqlJdbcTemplate.queryForList("select skuno,title,price,picurl,setdate from goods_info order by skuno asc");
	}
	
	

}
